/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package z.h.w.jar.arithmetic.method;

/**
 *
 * @author EnzoZhong @date 2011.10.30
 */
public class SearchDichotomy implements SearchMethodApi {

     /**
      *
      * @param <Type>
      * @param array
      * @param target
      * @return
      */
     @Override
     public <Type extends Comparable<? super Type>> Integer search(Type[] array, Type target) {
          int start = 0;
          int end = array.length - 1;
          int now;
          while (true) {
               now = (start + end) / 2;
               if (target.compareTo(array[now]) == 0) {
                    return now;
               } else if (start > end) {
                    return -1;
               } else {
                    if (target.compareTo(array[now]) > 0) {
                         start = now + 1;
                    } else {
                         end = now - 1;
                    }
               }
          }
     }
}
